<template>
  <div>
    <!-- 工作日历, 年和月 -->
    <div class="select-box">
      <el-select v-model="currentYear" size="small" style="width: 120px; margin-right: 10px" @change="changeDate">
        <!-- 年份取给定年份的, 前五年 + 后五年 -->
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>

      <el-select v-model="currentMonth" size="small" style="width: 120px;" @change="changeDate">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>

      <el-calendar v-model="currentDate">
        <template #dateCell="{ data ,date}">
          <div class="date-content">
            <span class="text">{{ data.day | getDay }}</span>
            <span v-if="isWeek(date)" class="rest">休</span>
          </div>
        </template>
      </el-calendar>
    </div>
  </div>
</template>

<script>
export default {
  filters: {
    getDay(value) {
      // 目的把date如果有0，应该去掉
      const day = value.split('-')[2]
      return day.startsWith('0') ? day.slice(1) : day
    //   console.log(day)
    }
  },
  props: {
    startDate: {
      type: Date,
      default: () => {
        return new Date()
      }
    }
    // startDate: {
    //   type: Date,
    //   default: new Date()
    // }
  },
  data() {
    return {
      currentYear: null, // 当前年份
      currentMonth: null, // 当前月份
      currentDate: null
    //   yearList: [] // 要遍历的年的数组
    }
  },
  //  <!-- 年份取给定年份的, 前五年 + 后五年 -->
  computed: {
    yearList() {
      return Array.from(new Array(11)).map((item, index) => { return this.currentYear + index - 5 })
    }
  },
  watch: {
    currentDate(newValue) {
      console.log('bianchal')
      console.log(newValue)
      this.currentYear = newValue.getFullYear() // 得到当前年份
      this.currentMonth = newValue.getMonth() + 1 // 当前月份
    //   不要再watch的对象中改变 对象的值  否则会陷入死循环
    //   this.currentDate = new Date(this.currentYear + '-' + this.currentMonth + '-1')
    }
  },
  created() {
    this.currentYear = this.startDate.getFullYear() // 得到当前年份
    this.currentMonth = this.startDate.getMonth() + 1 // 当前月份
    this.changeDate()
  },
  methods: {
    changeDate() {
    // 生成新的日期
      this.currentDate = new Date(this.currentYear + '-' + this.currentMonth + '-1')
    },
    isWeek(date) {
      if (date.getDay() === 6 || date.getDay() === 0) {
        return true
      }
    }
  }
}
</script>
<style lang="scss" scoped>
// .select-box {
//   display: flex;
//   justify-content: flex-end;
// }

::v-deep .el-calendar-day {
  height:  auto;
 }
::v-deep .el-calendar-table__row td::v-deep .el-calendar-table tr td:first-child, ::v-deep .el-calendar-table__row td.prev{
  border: none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
::v-deep .el-calendar__header {
   display: none
}
</style>

